home *** CD-ROM | disk | FTP | other *** search
/ Power CD / Power CD ATARI-Rechner Lieben.iso / SPEZIAL / GEMVIEW / UTILITY / GEM_XACC.101 / GEM_XACC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-20  |  6.2 KB  |  330 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <tos.h>
  5. #include <aes.h>
  6. #include "xacc.h"
  7.  
  8. #define WAIT_TIMER    10000
  9. #define  XNAME       "Test-xAcc\0XDSC\0"\
  10.                      "1xAcc-TEST-Accessory\0\0"
  11.  
  12.  
  13.  
  14.  
  15. int                 appl_id, menu_id,
  16.                  msg[8], ToAccApp = 0;
  17. long             tmp_count = 0;
  18. static char        *XAccName = XNAME;
  19.  
  20.  
  21. /* ----- Cookie Jar -------------------------------------------------------- */
  22.  
  23. typedef struct
  24. {
  25.     long    id,
  26.             *ptr;
  27. } COOKJAR;
  28.  
  29.  
  30.  
  31.  
  32. void XAccSend(int sendto, int msg0, int msg3, char *msg4, int msg6, int msg7)
  33. {
  34.   int     msg[8];
  35.   
  36.   msg[0] = msg0;
  37.   msg[1] = appl_id;
  38.   msg[2] = 0;
  39.   msg[3] = msg3;
  40.   *(char**)&msg[4] = msg4;
  41.   msg[6] = msg6;
  42.   msg[7] = msg7;
  43.   appl_write(sendto, 16, msg);
  44. }
  45.  
  46.  
  47.  
  48. void XAccSendId(int sendto)
  49. {
  50.   XAccSend(sendto, ACC_ID, 0x0103, XAccName, menu_id, -1);
  51. }
  52.  
  53.  
  54.  
  55. void XAccSendAck(int sendto, int answer)
  56. {
  57.   XAccSend(sendto, ACC_ACK, answer, NULL, -1, -1);
  58. }
  59.  
  60.  
  61.  
  62. void XAccSendAccClose(void)
  63. {
  64.   if (!_app && !(_GemParBlk.global[0] >= 0x0400 && _GemParBlk.global[1] == -1))
  65.     XAccSendId(ToAccApp);
  66. }
  67.  
  68.  
  69.  
  70. void XAccSendStartup(void)
  71. {
  72.   int     next, type, id;
  73.   char    name[10], *buf;
  74.   
  75.   if (_GemParBlk.global[0] >= 0x0400 && get_cookie('MiNT') != NULL) {
  76.     buf = Mxalloc(sizeof(XNAME), 0x0022);
  77.     if (buf != NULL)
  78.       memcpy(buf, XAccName, sizeof(XNAME));
  79.     XAccName = buf;
  80.     next = 0;
  81.     while (appl_search(next, name, &type, &id)) {
  82.       if (type & 0x06)
  83.         XAccSendId(id);
  84.       next = 1;
  85.     }
  86.   } else {
  87.     if (!_app)
  88.       XAccSendAccClose();
  89.   }
  90. }
  91.  
  92.  
  93.  
  94. long *get_cookie(long cookie)
  95. {
  96.     long    sav;
  97.     COOKJAR    *cookiejar;
  98.     int        i = 0;
  99.  
  100.     sav = Super((void *)1L);
  101.     if(sav == 0L)
  102.         sav = Super(0L);
  103.     cookiejar = *((COOKJAR **)0x05a0L);
  104.     if(sav != -1L)
  105.         Super((void *)sav);
  106.     if(cookiejar)
  107.     {
  108.         while(cookiejar[i].id)
  109.         {
  110.             if(cookiejar[i].id == cookie)
  111.                 return(cookiejar[i].ptr);
  112.             i++;
  113.         }
  114.     }
  115.     return(0L);
  116. }
  117.  
  118.  
  119.  
  120. void getfile(char **input, long *length)
  121. {
  122.     char    path[220];
  123.     char    file[20], *s;
  124.     int     ret, ex, fh;
  125.     
  126.     *input = NULL;
  127.     *length = 0;
  128.     path[0] = '\0';
  129.     file[0] = '\0';
  130.     ret = fsel_input(path, file, &ex);
  131.     if (ret == 0 || ex == 0)
  132.         return;
  133.     
  134.     s = strrchr(path, '\\');
  135.     if (s == NULL)
  136.         return;
  137.     
  138.     strcpy(s+1, file);
  139.     fh = (int)Fopen(path, FO_READ);
  140.     if (fh < 0)
  141.         return;
  142.     
  143.     *length = Fseek(0L, fh, SEEK_END);
  144.     Fseek(0L, fh, SEEK_SET);
  145.     *input = malloc(*length + 10L);
  146.     if (*input == NULL) {
  147.         Fclose(fh);
  148.         return;
  149.     }
  150.     
  151.     memset(*input, '\0', *length + 10L);
  152.     Fread(fh, *length, *input);
  153.     Fclose(fh);
  154. }
  155.  
  156.  
  157.  
  158.  
  159. XAccWait(int *msg, int wait)
  160. {
  161.     int        event, dummy;
  162.   
  163.     msg[0] = 0;
  164.     event = evnt_multi(MU_MESAG|MU_TIMER, 0, 0, 0,
  165.                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  166.                         msg, wait, 0, &dummy, &dummy,
  167.                         &dummy, &dummy, &dummy, &dummy);
  168.     if (event & MU_MESAG) {
  169.         if (msg[0] == 0x0500 && msg[3] == 1)
  170.             return(1);
  171.         if (msg[0] == AC_OPEN)
  172.             return(0);
  173.         if (msg[0] == AC_CLOSE)
  174.             return(0);
  175.         if (msg[0] == ACC_TEXT)
  176.             return(0);
  177.         if (msg[0] == ACC_IMG)
  178.             return(0);
  179.         if (msg[0] == ACC_META)
  180.             return(0);
  181.     }
  182.     if (!(event & MU_MESAG))
  183.         tmp_count++;
  184.     
  185.     return(0);
  186. }
  187.  
  188. void main(void)
  189. {
  190.     char   *input, *mem;
  191.     char    file[30];
  192.     int     fd;
  193.     long    length;
  194.     
  195.     tmp_count = 0;
  196.     appl_id = appl_init();
  197.     menu_register(appl_id, "  Send xAcc!");
  198.     XAccSendStartup();
  199.     msg[0] = 0;
  200.     while(1)
  201.     {
  202.         if (msg[0] == 0)
  203.             evnt_mesag(msg);
  204.         switch(msg[0])
  205.         {
  206.             case AC_OPEN:
  207.                 msg[0] = 0;
  208.                 switch(form_alert(0, "[0][ | Send xACC! | ][ Text | IMG | Meta ]"))
  209.                 {
  210.                     case 1:
  211.                         getfile(&input, &length);
  212.                         if (input == NULL)
  213.                         {
  214.                             msg[0] = 0;
  215.                             break;
  216.                         }
  217.                         input[length] = '\0';
  218.                         /* Länge ist bei Textfile eigentlich unnötig! */
  219.                         XAccSend(ToAccApp, ACC_TEXT, 1, input, (int)(length>>16), (int)(length & 0xFFFFL));
  220.                         XAccWait(msg, WAIT_TIMER);
  221.                         free(input);
  222.                         msg[0] = 0;
  223.                         break;
  224.                     case 2:
  225.                         getfile(&input, &length);
  226.                         if (input == NULL)
  227.                         {
  228.                             msg[0] = 0;
  229.                             break;
  230.                         }
  231.                         mem = input;
  232.                         while(length > 4096)
  233.                         {
  234.                             XAccSend(ToAccApp, ACC_IMG, 0, input, 0, 4096);
  235.                             if (XAccWait(msg, WAIT_TIMER) <= 0)
  236.                                 goto skip2;
  237.                             input += 4096;
  238.                             length -= 4096;
  239.                         }
  240.                         XAccSend(ToAccApp, ACC_IMG, 1, input, 0, (int)length);
  241.                         XAccWait(msg, WAIT_TIMER);
  242. skip2:
  243.                         free(mem);
  244.                         msg[0] = 0;
  245.                         break;
  246.                     case 3:
  247.                         getfile(&input, &length);
  248.                         if (input == NULL)
  249.                         {
  250.                             msg[0] = 0;
  251.                             break;
  252.                         }
  253.                         mem = input;
  254.                         while(length > 4096)
  255.                         {
  256.                             XAccSend(ToAccApp, ACC_META, 0, input, 0, 4096);
  257.                             if (XAccWait(msg, WAIT_TIMER) <= 0)
  258.                                 goto skip3;
  259.                             input += 4096;
  260.                             length -= 4096;
  261.                         }
  262.                         XAccSend(ToAccApp, ACC_META, 1, input, 0, (int)length);
  263.                         XAccWait(msg, WAIT_TIMER);
  264. skip3:
  265.                         free(mem);
  266.                         msg[0] = 0;
  267.                         break;
  268.                 }
  269.                 msg[0] = 0;
  270.                 break;
  271.             
  272.             case AC_CLOSE:
  273.                 XAccSendAccClose();
  274.                 msg[0] = 0;
  275.                 break;
  276.             
  277.             case ACC_ID:
  278.                 if (msg[1] != 0)
  279.                     ToAccApp = msg[1];
  280.                 break;
  281.             
  282.             case ACC_ACC:
  283.                 if (msg[7] != 0)
  284.                     ToAccApp = msg[7];
  285.                 if (!_app && !(_GemParBlk.global[0] >= 0x0400 && _GemParBlk.global[1] == -1))
  286.                     XAccSendId(msg[7]);
  287.                 msg[0] = 0;
  288.                 break;
  289.             
  290.             case ACC_TEXT:
  291.                 sprintf(file, "C:\\CLIPBRD\\TMP%05lx.TXT", tmp_count);
  292.                 if (msg[3] == 1)
  293.                     tmp_count++;
  294.                 goto save;
  295.             
  296.             case ACC_IMG:
  297.                 sprintf(file, "C:\\CLIPBRD\\TMP%05lx.IMG", tmp_count);
  298.                 if (msg[3] == 1)
  299.                     tmp_count++;
  300.                 goto save;
  301.             
  302.             case ACC_META:
  303.                 sprintf(file, "C:\\CLIPBRD\\TMP%05lx.GEM", tmp_count);
  304.                 if (msg[3] == 1)
  305.                     tmp_count++;
  306. save:
  307.                 fd = (int)Fopen(file, FO_WRITE);
  308.                 if (fd < 0)
  309.                     fd = (int)Fcreate(file, 0x00);
  310.                 if (fd > 0)
  311.                 {
  312.                     Fseek(0, fd, SEEK_END);
  313.                     Fwrite(fd, *(long*)&msg[6],*(char**)&msg[4]);
  314.                     Fclose(fd);
  315.                     XAccSendAck(msg[1], 1);
  316.                 }
  317.                 else
  318.                 {
  319.                     XAccSendAck(msg[1], 0);
  320.                     tmp_count++;
  321.                 }
  322.                 msg[0] = 0;
  323.                 break;
  324.             
  325.             default:
  326.                 msg[0] = 0;
  327.         }
  328.     }
  329. }
  330.